#include "timer.h"


namespace kanon
{
    PeriodicUpdater::PeriodicUpdater(float period)
    {
        this->period = period;
        prev_time = this->GetTime();
        accu_time = prev_time;
        prev_time1 = prev_time;
    }

    uint32_t PeriodicUpdater::Update()
    {
        float cur_time = this->GetTime();
        
        uint32_t nperiods = static_cast<uint32_t>((cur_time - prev_time) / period);
        prev_time = prev_time + nperiods * period;
        return nperiods;
    }

    uint32_t PeriodicUpdater::Update(float deltaTime)
    {
        accu_time += deltaTime;
        uint32_t nperiods = static_cast<uint32_t>((accu_time - prev_time1) / period);
        prev_time1 = prev_time1 + nperiods * period;
        return nperiods;
    }

    float PeriodicUpdater::GetTime()
    {
        clock_t cur = clock();
        float t = cur * 1.0f / CLOCKS_PER_SEC;
        return t;
    }
}